// component/scollList/scrollList.js
var app = getApp();
var listJs = require('../../../utils/list.js');
Component({
  options: {
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    listOptions: {
      type: Object,
      value: null,
      observer: function(newVal, oldVal) {
        if (newVal) {
          this.show();
        } else {
          this.clear();
        }
      }
    },
    scrollHeight: {
      type: Number,
      value: 650,
    },
    lowerThreshold: {
      type: Number,
      value: 250,
    },
    customized: { //无数据时的是否自定义UI
      type: Boolean,
      value: false
    },
    scrollY: {
      type: Boolean,
      value: true
    },
    float: {
      type: Boolean,
      value: false
    },
    showLogo: { //列表背景是否显示"玩购"logo
      type: Boolean,
      value: false,
    },
    check: { //数据检查，保证在网络不好的情况下能够自动加载数据
      type: Boolean,
      value: false,
      observer: function(oldVal, newVal) {
        if (newVal === true) {
          this.checkData();
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    scrollTop: 0,
    loading: false,
    nomore: false,
    nodata: false,
    nosignal: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 显示列表
     */
    show: function() {
      this.clear();
      //列表参数初始化
      this.listId = this.data.listOptions.list_id || '';
      listJs.init(this.listId, {
        url: this.data.listOptions.url,
        data: this.data.listOptions.data,
      })
      //加载第一页
      this.load();
    },
    /**
     * 清理列表
     */
    clear: function() {
      //组件参数初始化
      this.listData = [];
      this.setData({
        scrollTop: 0,
        loading: false,
        nomore: false,
        nodata: false,
        nosignal: false,
      })
      this.listId = '';
    },
    /**
     * 检查数据，如果无网络、无数据，重新加载数据
     */
    checkData: function() {
      if (this.data.nosignal && this.data.nodata && this.listId) {
        this.load();
      }
    },

    /**
     * 加载下一页
     */
    load: function() {
      if (!this.listId) {
        return;
      }
      if (this.data.loading || this.data.nomore) {
        return;
      }
      this.setData({
        loading: true,
      })
      listJs.load(this.listId, res => {
        var nomore = false;
        var nodata = false;
        var listData = this.listData || [];
        if (res.length > 0) {
          this.listData = listData.concat(res);
        } else {
          nomore = true;
          console.log('无更多加载数据');
        }
        if (this.listData.length == 0) {
          nodata = true;
        }

        if (this.data.loading) {
          this.triggerEvent('loadmore', {
            list_id: this.listId,
            data: this.listData,
          }, {});

          //为上级页面重新渲染数据提供一定时间
          setTimeout(() => {
            this.setData({
              loading: false,
              nomore: nomore,
              nodata: nodata,
              nosignal: false,
            })
          }, 500);
        }
      }, (err_msg, code) => {
        var nosignal = false;
        var nodata = false;
        if (code == -1 || app.globalData.isConnected === false) {
          nosignal = true;
        }
        if (this.listData.length == 0) {
          nodata = true;
        }
        this.setData({
          message: err_msg,
          loading: false,
          nosignal: nosignal,
          nodata: nodata,
        })
      });
    },

    /**
     * 重新加载数据
     */
    reload: function() {

    },

    /**
     * 滚动加载
     */
    scroolToLower: function() {
      if (this.data.nosignal == true && this.data.nodata == true) {
        return;
      }

      this.load();
    },

    /**
     * 滚动事件
     */
    scroll: function(e) {
      var scrollTop = e.detail.scrollTop;
      this.triggerEvent('scrollDistance', {
        list_id: this.listId,
        distance: scrollTop,
      }, { bubbles: true, composed: true });
    },
  }
})